using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using umbraco.cms.presentation.Trees;
using umbraco.interfaces;
using umbraco.controls;
using umbraco.cms.businesslogic.language;
using umbraco.BusinessLogic.Actions;
using Commerce.Common;

namespace CommerceForUmbraco.adminSection.trees
{
        /// <summary>
        /// Renders a Tree containing all Categories
        /// </summary>
        public class loadCategoryTree : BaseTree
        {
            public loadCategoryTree(string application) : base(application) { }

            public override void RenderJS(ref StringBuilder Javascript)
            {
                Javascript.Append(
                    @"
                    function openCategory(id) {
	                    parent.right.document.location.href = 'commerce/editCategory.aspx?c=' + id;
                    }
                ");
            }

            public override void Render(ref XmlTree tree)
            {
                // get collection values
                Commerce.Common.CategoryCollection categories = null;

                categories = new CategoryCollection().OrderByAsc("categoryName").Load();

                foreach (Category thisCat in categories)
                {
                    XmlTreeNode xNode = XmlTreeNode.Create(this);
                    xNode.NodeID = thisCat.CategoryID.ToString();
                    xNode.Text = thisCat.CategoryName;
                    xNode.Action = "javascript:openCategory(" + thisCat.CategoryID + ");";
                    xNode.Icon = "statistik.gif";
                    xNode.OpenIcon = "statistik.gif";

                    tree.Add(xNode);
                }
            }

            protected override void CreateRootNode(ref XmlTreeNode rootNode)
            {
                rootNode.Icon = FolderIcon;
                rootNode.OpenIcon = FolderIconOpen;
                rootNode.NodeType = "init" + TreeAlias;
                rootNode.NodeID = "init";
            }

            protected override void CreateAllowedActions(ref System.Collections.Generic.List<IAction> actions)
            {
                actions.Clear();
                actions.Add(ActionRefresh.Instance);
            }

            protected override void CreateRootNodeActions(ref System.Collections.Generic.List<IAction> actions)
            {
                actions.Clear();
                actions.Add(ActionNew.Instance);
                actions.Add(ActionRefresh.Instance);
            }
        }
    }
